package com.bitantenna.streams.audio;

import javazoom.spi.vorbis.sampled.file.VorbisAudioFileReader;

import org.xiph.libogg.ogg_packet;
import org.xiph.libogg.ogg_page;
import org.xiph.libogg.ogg_stream_state;
import org.xiph.libogg.ogg_sync_state;
import org.xiph.libvorbis.vorbis_block;
import org.xiph.libvorbis.vorbis_comment;
import org.xiph.libvorbis.vorbis_dsp_state;
import org.xiph.libvorbis.vorbis_info;

import com.bitantenna.messages.MessageFrame;
import com.bitantenna.streams.StreamOutput;


public class OggToPcmOutput implements StreamOutput {

	PcmPlayerOutput pcmOutput;


	public OggToPcmOutput(PcmPlayerOutput pcmOutput) {
		this.pcmOutput=pcmOutput;			


	}


	@Override
	public void writeHeader(MessageFrame header) {
		new VorbisAudioFileReader();
	}
	
	  ogg_sync_state   oggSyncState; /* sync and verify incoming physical bitstream */
	  ogg_stream_state oggStreamState; /* take physical pages, weld into a logical
	                          stream of packets */
	  ogg_page         oggPage; /* one Ogg bitstream page. Vorbis packets are inside */
	  ogg_packet       oggPacket; /* one raw packet of data for decode */

	  vorbis_info      vorbisInfo; /* struct that stores all the static vorbis bitstream
	                          settings */
	  vorbis_comment   vorbisComment; /* struct that stores all the bitstream user comments */
	  vorbis_dsp_state vorbisDspState; /* central working state for the packet->PCM decoder */
	  vorbis_block     vorbisBlock; /* local working space for packet->PCM decode */

	@Override
	public void writeSample(MessageFrame f) {
		  byte[] sample=f.payloadBytes();
		
		  oggSyncState=new ogg_sync_state(); /* Now we can read pages */
		  oggStreamState=new ogg_stream_state();
		  oggPage=new ogg_page();
		  oggPacket=new ogg_packet();
		  vorbisInfo=new vorbis_info();
		  vorbisComment=new vorbis_comment();
		  
		  vorbisDspState=new vorbis_dsp_state();
		  vorbisDspState.vorbis_analysis_init(vorbisInfo);
		  		 
		  vorbisBlock=new vorbis_block(vorbisDspState);
		  
		  vorbisBlock.vorbis_analysis(oggPacket);		  
	}


	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void open() {
		// TODO Auto-generated method stub
		
	}
	


}
